package com.wzz.sparkDemo

import com.mysql.jdbc.Driver
import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

import java.sql
import java.sql.{Connection, DriverManager, PreparedStatement}

object RddDemo {

  def main(args: Array[String]): Unit = {
    val sparkconf: SparkConf = new SparkConf().setMaster("local[3]").setAppName("sdfs")
    val sc: SparkContext = SparkContext.getOrCreate(sparkconf)



    val data = sc.parallelize(List(("zhangsan",12), ("lisi",13),("wangwu",14)),2)
    data.foreach(d => {
      val conn: Connection = sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root")
      val statement: PreparedStatement = conn
        .prepareStatement("insert into person values(?, ?)")
      statement.setString(1, d._1)
      statement.setInt(2, d._2)
      statement.executeUpdate()
      conn.close()
    })



  }

}
